package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PhysiqueAssessmentDao;
import com.itheima.dao.PhysiqueDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.PhyQuestion;
import com.itheima.pojo.Physique;
import com.itheima.pojo.PhysiqueAssessment;
import com.itheima.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查组CRUD操作业务实现类
 *
 * @author W.J.Y
 */
@Service(version = "1.0.0", interfaceClass = PhysiqueAssessmentService.class)
@Transactional
public class PhysiqueAssessmentServiceImpl implements PhysiqueAssessmentService {


    @Resource
    private PhysiqueDao physiqueDao;
    @Resource
    private PhysiqueAssessmentDao physiqueAssessmentDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage,pageSize);
        //调用dao层查询语句
        Page<PhysiqueAssessment> page = physiqueAssessmentDao.findPage(queryString);
        for (PhysiqueAssessment physiqueAssessment : page) {
            Date regTime = physiqueAssessment.getRegTime();
            String formatTime = new SimpleDateFormat("yyyy-MM-dd").format(regTime);
            physiqueAssessment.setFormatTime(formatTime);
        }
        //封装分页查询结果对象
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询所有体质
     *
     * @return 体质集合
     */
    public List<Physique> findAllPhy() {
        List<Physique> physiqueList = physiqueDao.findAllPhy();
        for (Physique physique : physiqueList) {
            List<PhyQuestion> phyQuestionList = physiqueDao.findPhyQuestionByPid(physique.getId());
            physique.setQuestionList(phyQuestionList);
        }
        return physiqueList;
    }

    /**
     * 删除体质测评
     * @param id 体质测评id
     */
    @Override
    public void delete(int id) throws BusinessException {
        physiqueAssessmentDao.delete(id);
    }

    /**
     * 根据ID查询体质测评
     * @return 体测实例
     */
    @Override
    public PhysiqueAssessment findByFileId(String id) {
        return physiqueAssessmentDao.findByFileId(id);
    }

    /**
     * 根据ID查询体质测评
     *
     * @return 体测实例
     */
    @Override
    public PhysiqueAssessment MobileFindByFileId(String id) {
        return physiqueAssessmentDao.findByFileId(id);
    }

    @Override
    public PhysiqueAssessment findById(Integer id) {
        return physiqueAssessmentDao.findById(id);
    }

}
